package org.feng.framework.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.feng.utils.HttpUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 检测接口响应时间
 *
 * @author feng
 */
@Aspect
@Slf4j
@Component
public class SlowApiAspect {

    private static final Long OVER_TIME = 3000L;

    @Around("execution(* org.feng.controller..*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        HttpServletRequest request = HttpUtils.getRequest();
        Object proceed = pjp.proceed();
        long end = System.currentTimeMillis();
        if ((end - start) > OVER_TIME) {
            log.error("{},接口响应过慢：{}ms", request.getRequestURI(), end - start);
        }
        return proceed;
    }
}
